package com.durian.base.frame.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;

import com.durian.base.frame.activity.ActivityFrame;
import com.durian.base.utils.ToastUtils;
import com.durian.base.utils.ToolUtils;


/**
 * 说明：SupportFragment
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2015/10/28 21:57
 * <p/>
 * 版本：version 1.0
 */

public abstract class SupportFragment extends Fragment {

    protected ActivityFrame mActivityFrame;
    protected View          fragmentRootView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setViewBefor(context);
    }

    private View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        if (getActivity() != null && getActivity() instanceof ActivityFrame) {
            mActivityFrame = (ActivityFrame) getActivity();
        } else {
            throw new RuntimeException("SupportFragment中获取Activity不是ActivityFrame,请在ActivityFrame中使用");
        }
        View rootView = bindRootView(mActivityFrame);
        if (rootView != null) {
            return rootView;
        }
        return inflater.inflate(getRootViewResID(), null);
    }

    public ActivityFrame activity() {
        return mActivityFrame;
    }

    public void onBackPressed() {
        if (mActivityFrame != null) {
            mActivityFrame.onBackPressed();
        }
    }

    public void finish() {
        if (mActivityFrame != null && ToolUtils.isNotFinish(mActivityFrame)) {
            mActivityFrame.finish();
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    protected int getRootViewResID() {
        return 0;
    }

    protected View bindRootView(Context context) {
        return null;
    }

    /**
     * 说明：获取数据
     */
    protected void getBundleData(Bundle bundle) {
    }

    /**
     * 说明：初始化数据onActivityCreated
     */
    protected void onInit(Bundle savedInstanceState, View view) {
    }

    /**
     * 说明：初始化数据onCreateView
     */
    protected void onInitCreateView(View view) {
    }

    /**
     * 说明：在绑定数据之前调用
     *
     * @param context
     */
    protected void setViewBefor(Context context) {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (fragmentRootView == null) {
            fragmentRootView = inflaterView(inflater, container, savedInstanceState);
        } else {
            ViewGroup parent = (ViewGroup) fragmentRootView.getParent();
            if (parent != null) {
                parent.removeView(fragmentRootView);
            }
        }
        onInitCreateView(fragmentRootView);
        return fragmentRootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (getArguments() != null) {
            getBundleData(getArguments());
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        fragmentRootView = null;
        super.onDestroyView();
    }

    public <T extends View> T findViewById(@IdRes int id) {
        return fragmentRootView != null ? fragmentRootView.findViewById(id) : null;
    }

    /**
     * Toast
     **/

    public void shortToast(int res) {
        ToastUtils.get().shortToast(res);
    }

    public void shortToast(String res) {
        ToastUtils.get().shortToast(res);
    }

    public void longToast(String res) {
        ToastUtils.get().longToast(res);
    }

    public void longToast(int res) {
        ToastUtils.get().longToast(res);
    }

    public void cancelToast() {
        ToastUtils.get().cancelToast();
    }

    /**Toast**/
}

